package com.dding.cms.base.service.impl;

import static com.dding.cms.base.web.ActionConstants.DEFAULT_CHANGE_FIELDS;
import static com.dding.cms.base.web.ActionConstants.PAGE_MODEL_KEY;
import static com.dding.cms.base.web.ActionConstants.VALUE_RESULT_KEY;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dding.core.log.ILogger;
import org.dding.core.log.LoggerFactory;
import org.dding.core.util.StringUtils;
import org.javalite.activejdbc.Base;
import org.javalite.activejdbc.Model;

import com.dding.cms.base.dao.IDaoHandler;
import com.dding.cms.base.dao.impl.ContentEditDaoHandler;
import com.dding.cms.base.dao.impl.ContentStoreDaoHandler;
import com.dding.cms.base.dao.impl.ContentUpdateDaoHandler;
import com.dding.cms.base.dao.impl.PageDaoImpl;
import com.dding.cms.base.entity.Content;
import com.dding.cms.base.service.ContentService;

public class ContentServiceImpl extends PageDaoImpl implements ContentService {

	private ILogger logger = LoggerFactory.getLogger(getClass());
	IDaoHandler<String[], String[]> storeHandler = new ContentStoreDaoHandler();
	IDaoHandler<Object, Object> editHandler = new ContentEditDaoHandler();
	IDaoHandler<String[], String[]> updateHandler = new ContentUpdateDaoHandler();

	@Override
	public void storeContent(Map<String, String[]> contentMap) {
		Content model = new Content();
		List<Model> list = new ArrayList<Model>();
		try {
			Base.openTransaction();
			storeHandler.handleField(contentMap, model);
			storeHandler.handleAssocaition(contentMap, list, model);
			storeHandler.handleExtAttr(contentMap, list, model);

			model.save();
			for (Model child : list) {
				model.add(child);
			}
			Base.commitTransaction();
		} catch (Throwable e) {
			Base.rollbackTransaction();
			logger.error(e);
		} finally {
			Base.close();
		}
	}

	@Override
	public void updateContent(Map<String, String[]> contentMap) {

		if (contentMap.get(DEFAULT_CHANGE_FIELDS) == null) {
			return;
		}

		String id = contentMap.get("content_id")[0];
		if (StringUtils.isEmpty(id)) {
			return;
		}

		try {
			Base.openTransaction();
			updateHandler.handleField(contentMap, null);
			updateHandler.handleAssocaition(contentMap, null, null);
			updateHandler.handleExtAttr(contentMap, null, null);
			Base.commitTransaction();
		} catch (Throwable e) {
			Base.rollbackTransaction();
			logger.error(e);
		} finally {
			Base.close();
		}

	}

	@Override
	public Map<String, Map<String, Object>> editContent(
			Map<String, String[]> contentMap) {

		Map<String, Map<String, Object>> result = new HashMap<String, Map<String, Object>>();
		String id = contentMap.get("id")[0];
		if (StringUtils.isEmpty(id)) {
			return result;
		}
		Model model = Content.findById(id);
		Map<String, Object> pageModel = getPageModel(contentMap);
		result.put(PAGE_MODEL_KEY, pageModel);
		try {
			Base.openTransaction();
			Map<String, Object> valueResult = editHandler.handleField(null,
					model);
			valueResult
					.putAll(editHandler.handleAssocaition(null, null, model));
			valueResult.putAll(editHandler.handleExtAttr(null, null, model));
			result.put(VALUE_RESULT_KEY, valueResult);
		} catch (Throwable e) {
			Base.rollbackTransaction();
			logger.error(e);
		} finally {
			Base.close();
		}
		return result;
	}

}
